//@author A0105546J

package tasky.logic;

import java.util.ArrayList;

import tasky.commons.Task;
import tasky.history.History;
import tasky.history.HistoryStatus;

public class RedoCommand {

	private static final String NO_MORE_REDO = "No more operation can be redone\n";
	private static final String HAS_BEEN_REDONE = " has been redone\n";
	private static final String COMMAND_REDO = "redo";

	private History history;

	public RedoCommand(History h) {

		history = h;

	}

	public UndoAndRedoResult execute(ArrayList<Task> tasks) {

		ArrayList<Task> newTasks = new ArrayList<Task>();

		HistoryStatus historyStatus = history.redo(tasks);

		// if there is no more redo can be performed.
		if (historyStatus == null) {

			UndoAndRedoResult undoAndRedoResult = new UndoAndRedoResult(
					new Result(NO_MORE_REDO, COMMAND_REDO, false), null);

			return undoAndRedoResult;
		}

		// output the redone command name and the new version of task list.
		String output = "";

		output = historyStatus.getCommand() + HAS_BEEN_REDONE;

		for (int i = 0; i < historyStatus.getTasks().size(); i++) {
			newTasks.add(historyStatus.getTasks().get(i));
		}

		tasks = newTasks;

		UndoAndRedoResult undoAndRedoResult = new UndoAndRedoResult(new Result(
				output, COMMAND_REDO, false), tasks);

		return undoAndRedoResult;

	}

}